package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCsunSleepDataMapper;
import com.csun.cmny.provider.device.csun.sleep.entity.CsunSleepData;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcCsunSleepData;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.dto.sleep.BedOldmanSleepReqDto;
import com.csun.cmny.provider.model.vo.CsunSleepDataReqVo;
import com.csun.cmny.provider.model.vo.DmcDeviceAlert;
import com.csun.cmny.provider.vo.DmcCsunSleepDataVo;
import com.csun.cmny.provider.vo.RoomSleepInfo;
import com.csun.cmny.provider.vo.SleepDataVo;
import com.csun.cmny.provider.model.vo.csunsleep.CsunSleepRealDataVo;
import com.csun.cmny.provider.model.vo.csunsleep.DmcCsunSleepVo;
import com.csun.cmny.provider.model.vo.life.LifeSleepDataIndex;
import com.csun.cmny.provider.service.DmcCsunSleepBindService;
import com.csun.cmny.provider.service.DmcCsunSleepDataService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-02-15 11:04
 * @Description: the class dmc csun sleep data service impl
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcCsunSleepDataServiceImpl extends BaseService<DmcCsunSleepData>
        implements DmcCsunSleepDataService {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private DmcDeviceService dmcDeviceService;

    @Resource
    private DmcCsunSleepBindService dmcCsunSleepBindService;

    @Resource
    private DmcCsunSleepDataMapper dmcCsunSleepDataMapper;

    /**
     * query csun sleep real data by device id
     *
     * @param deviceId device id
     * @return the csun sleep real data vo
     */
    @Override
    public CsunSleepRealDataVo queryCsunSleepRealDataByDeviceId(Long deviceId) {

        // 1.校验智能床垫 - 日创
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsCsunSleepByDeviceId(deviceId);
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610034, deviceId);
        }

        // 2.判断校验智能床垫 - 日创是否已经绑定
        DmcCsunSleepVo dmcCsunSleepVo = dmcCsunSleepBindService.getCsunSleepBindByDeviceId(deviceId);
        if (PublicUtil.isEmpty(dmcCsunSleepVo)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610036, deviceId);
        } else {
            if (PublicUtil.isEmpty(dmcCsunSleepVo.getBindType())) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610036, deviceId);
            }
        }

        // 4.从redis中获取数据
        String csunSleepDataKey = RedisKeyUtil.getCsunSleepKey(dmcCsunSleepVo.getDeviceNo());
        CsunSleepRealDataVo csunSleepRealDataVo = new CsunSleepRealDataVo();
        CsunSleepData csunSleepData = (CsunSleepData) redisTemplate.opsForValue().get(csunSleepDataKey);

        BeanUtils.copyProperties(dmcCsunSleepVo, csunSleepRealDataVo);
        BeanUtils.copyProperties(csunSleepData, csunSleepRealDataVo);
        csunSleepRealDataVo.setBind(true);

        return csunSleepRealDataVo;
    }

    /**
     * query csun sleep data by bed oldman list
     *
     * @param bedOldmanSleepReqDtoList bed oldman sleep req dto list
     *
     * @return the list
     */
    @Override
    public List<CsunSleepDataReqVo> queryCsunSleepDataByBedOldmanList(List<BedOldmanSleepReqDto> bedOldmanSleepReqDtoList) {

        List<CsunSleepDataReqVo> csunSleepDataReqVoList = dmcCsunSleepBindService.queryCsunSleepDataByBedOldmanList(bedOldmanSleepReqDtoList);
        for (CsunSleepDataReqVo csunSleepDataReqVo : csunSleepDataReqVoList) {
            CsunSleepData csunSleepData = (CsunSleepData) redisTemplate.opsForValue().get(RedisKeyUtil.getCsunSleepKey(csunSleepDataReqVo.getDeviceNo()));
            if (PublicUtil.isNotEmpty(csunSleepData)) {
                BeanUtils.copyProperties(csunSleepData, csunSleepDataReqVo);
            }
        }

        return csunSleepDataReqVoList;
    }

    /**
     * 查询所有绑定了的日创床垫数据
     *
     * @return the list.
     */
    @Override
    public List<CsunSleepData> queryCsunSleepDataBindList() {

        return dmcCsunSleepDataMapper.queryCsunSleepDataBindList();
    }

    @Override
    public List<LifeSleepDataIndex> selectDataByRoomId(Long roomId) {
        return dmcCsunSleepDataMapper.selectDataByRoomId(roomId);
    }
    //查询床垫数据
    @Override
    public List<LifeSleepDataIndex> querySleepInfoByRoomId(ArrayList<Long> ids) {
        return dmcCsunSleepDataMapper.querySleepInfoByRoomId(ids);
    }
    //查询报警信息
    @Override
    public List<DmcDeviceAlert> querySleepAlertInfo(ArrayList<Long> ids, Integer isHandle) {
        return dmcCsunSleepDataMapper.querySleepAlertInfo(ids,isHandle);
    }
    //查询床垫状态
    @Override
    public List<SleepDataVo> queryInBed(List<Long> deviceIds) {
        return dmcCsunSleepDataMapper.queryInBed(deviceIds);
    }
    //查询绑定了床垫的老人
    @Override
    public List<RoomSleepInfo> querySleepBind(ArrayList<Long> ids) {
        return dmcCsunSleepDataMapper.querySleepBind(ids);
    }
    //查询床垫时间段是否在床
    @Override
    public List<CsunSleepDataReqVo> querySleepInbed(ArrayList<Long> ids, String time) {
        return dmcCsunSleepDataMapper.querySleepInbed(ids,time);
    }
    //查询报警信息
    @Override
    public List<DmcDeviceAlert> queryAllSleepAlert(ArrayList<Long> ids) {
        return dmcCsunSleepDataMapper.queryAllSleepAlert(ids);
    }
    //查询床垫状态
    @Override
    public List<DmcCsunSleepDataVo> querySleepDateById(List<DmcCsunSleepVo> dmcCsunSleepVos) {
        return dmcCsunSleepDataMapper.querySleepDateById(dmcCsunSleepVos);
    }
}
